//后台在线留言业务代码
const onlineMsgModel = require("../models/onlineMsg.js");
const moment = require("moment")
class onlineMsgController {
    //显示留言列表
    static async showList(req, res) {
        let { page = 1, size = 5 } = req.query
        page = parseInt(page);
        size = parseInt(size);
        let count = await onlineMsgModel.count();
        let offset = (page - 1) * size;
        let totalPage = Math.ceil(count / size);
        let info = await onlineMsgModel.find().skip(offset).limit(size);
        res.render("onlineMsg-list", { title: "留言列表", info, moment, page, size, totalPage })
    }
    //显示留言详情
    static async showDetail(req, res) {
        let info = await onlineMsgModel.findById(req.params.id)
        res.render("onlineMsg-detail", { title: "留言详情", info, moment })
    }
    //删除留言
    static async delMsg(req, res) {
        try {
            await onlineMsgModel.findByIdAndDelete({ _id: req.params.id })
        } catch (e) {
            console.log(e);
        } finally {
            res.redirect("back");
        }

    }
}

module.exports = onlineMsgController